<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="UTF-8"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ page isELIgnored="false"%>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">

<title>Ajax 1</title>
<link href="resources/css/style.css" rel="stylesheet">
<script src="/mohws/js/jquery.min.js"></script>
<script src="/mohws/js/jquery-ui.js"></script>

</head>
<body >



for (c in ${aslist})
   ${c};

5 Column Application Settings 
<c:url var="homeUrl" value="/" />
<c:url var="PatientNewRegUrl" value="/patients/reg1" />



First Name :<input type=text id=fname><br/>
Second Name :<input type=text id=sname>
	<br>etc
<input type=button onClick="fetchAppSettingList();" id="fSubmit" value="list">



  <div id="page_header" class="page_header">Page</div>
<br>  Patiend id
  <div id=statusD></div>
<br>
  
  <div id=dbg1>Debug <br></div><br>
  
  
   <div id=userInput></div>
   
   <br/>
   <div id="perslist" />
  <script>


var patsObj = JSON.parse("{\"errno\": \"4\",\"error\": \"Init error\"}"); // didnt understand this
function dbg(s, jj){
	d = document.getElementById("dbg1");
	s = s + "";
	x =  s.replace("</", "-");
	x = x.replace("<", "--");
	x = x.replace(">", "--");
	if(jj==2){
		alert(x);
	}
	d.innerHTML = d.innerHTML + "<br>" + x	;
}

function setInnerHtmlToObj(s, tos){
	d = document.getElementById(tos);
	d.innerHTML = s	;
}

// checkif survey exists. if not show error. if yes server will return info about it, which we will use to display this form
// todo error handling , timeout etc

var surveyCode = "11";
var contextPath = "/mohweb/";
//alert("ggg");
/*
asds
comment
*/
dbg("plac 1 ", 0)

setTimeout(fetchStatus, 2000);
function fetchStatus(){
	$.ajax({
	url: contextPath ,
	context: document.body,
	data : "fname=" + document.getElementById('fname').value + "&sname=" + document.getElementById('sname').value,  
	//surveyCode + ";name=ashiq" ,
	dataType : "json"
	}).success(function(data, txtSt, jqHr) {
		try{
			patsObj = data;
			ss = "Hello .." + patsObj.name;
			ss = ss + "<br/>Summary: " + patsObj.summary + "<br/>" + "Content: " + patsObj.content;
			setInnerHtmlToObj(ss, "statusD");
			setTimeout(fetchStatus, 5000);
		}catch(e){
			
		}
	}).error(function (jqXHR, textStatus, errorThrown){
		setInnerHtmlToObj("Network or Server Error", "page_header");
		document.userInput.style.display = "none";

		dbg("get suvery details error :  " + errorThrown + ", " + textStatus + ", " + jqXHR, 0);
	
	});
}

//setTimeout(fetchPersonList, 5000);
function fetchAppSettingList(){
	alert("fetchAppSettingList() callled on BUtton CLick");
	$.ajax({
	url: contextPath ,
	context: document.body,
	data : "",    
	//"fname=" + document.getElementById('fname').value + "&sname=" + document.getElementById('sname').value,  
	//surveyCode + ";name=ashiq" ,
	dataType : "json"
	}).success(function(data, txtSt, jqHr) {
		try{
			
			s = data;
			/*$.each(data, function(k, v){
				s = s + "<br/>[" +  v.mainId + "] "  + v.subId + ", " + v.paramName; // v.pid + "] "  + v.firstName ", " + v.secondName;
		    });
			*/
			setInnerHtmlToObj(s, "perslist");
			
			/*patsObj = data;
			ss = "Hello .." + patsObj.name;
			ss = ss + "<br/>Summary: " + patsObj.summary + "<br/>" + "Content: " + patsObj.content;
			setInnerHtmlToObj(ss, "statusD");
			*/
			//setTimeout(fetchPersonList, 5000);
		}catch(e){
			
		}
	}).error(function (jqXHR, textStatus, errorThrown){
		setInnerHtmlToObj("Network or Server Error", "page_header");
		document.userInput.style.display = "none";

		dbg("get suvery details error :  " + errorThrown + ", " + textStatus + ", " + jqXHR, 0);
	
	});
}


function fetchSurveyDetails(){
	$.ajax({
	url: contextPath + "./sampleJson1.txt",
	context: document.body,
	data : "code=" + surveyCode ,
	dataType : "json"
	}).success(function(data, txtSt, jqHr) {
		try{
			patsObj = data;
			if(patsObj.errorNo == 0){
				//alert(patsObj.body2);
				setInnerHtmlToObj(patsObj.patients[0].id, "p0id");
				//setInnerHtmlToObj(patsObj.body1, "body1");
				
				//setInnerHtmlToObj(patsObj.logo, "company_Logo");
				//$('.container').css('background', 'url("resources/customers/'+patsObj.background+'") no-repeat scroll 0 0 rgba(0, 0, 0, 0)');
				//$('.brand-logo').append('<img src="resources/customers/'+ patsObj.logo +'">');
				if(patsObj.jsAdd != ''){
					var js = document.createElement('script');
					js.type = 'text/javascript';
	
					js.text = patsObj.jsAdd;
					var scr = document.getElementsByTagName('script')[1];
					scr.parentNode.insertBefore(js,scr);
					dbg("Js add done", 0);
				}
			}
			else {
				$('.page_header').css("color","red");				
				setInnerHtmlToObj(patsObj.error, "page_header");
			}
			
		}catch(e){
			
		}
	}).error(function (jqXHR, textStatus, errorThrown){
		setInnerHtmlToObj("Network or Server Error", "page_header");
		document.userInput.style.display = "none";

		dbg("get suvery details error :  " + errorThrown + ", " + textStatus + ", " + jqXHR, 0);
	
	});
}


function submitSurvey(){

	var fname = document.userInput.fname.value;
	var lname = document.userInput.lname.value;
	var email = document.userInput.email.value;
	var mobile = document.userInput.mobile.value;
	var rating = document.getElementById('product-rate1').value;//document.userInput.rating.value;
	var comments = document.userInput.comments.value;
	dbg("firstname " +fname, 0);
	
	var dataString = 'fname='+ fname + '&lname=' + lname + '&email=' + email + '&mobile=' + mobile + '&rating=' + rating + '&comments=' + comments+ '&surveyCode=' + surveyCode;
	$.ajax({
	url: contextPath + "/submitSurvey",
	context: document.body,
	data : dataString,
	dataType : "json"
	}).success(function(data, txtSt, jqHr) {
		try{
			patsObj = data;
			if(patsObj.errorNo == 0) {
			document.userInput.reset();
			$('.survey_thankyou').css("color","green");
			setInnerHtmlToObj(patsObj.thankyou, "survey_thankyou");
			} else {
				$('.page_header').css("color","red");
				setInnerHtmlToObj("Warning", "page_header");
				setInnerHtmlToObj(patsObj.error, "survey_thankyou");
			}
			
		}catch(e){
			dbg("get suvery details err :  " + e, 0);
		}
		
	}).error(function (jqXHR, textStatus, errorThrown){
		setInnerHtmlToObj("Network or Server Error in Submitting Survey", "survey_thankyou");
		dbg("get suvery details error :  " + errorThrown + ", " + textStatus + ", " + jqXHR, 0);
	
	});
}

</script>
  </body>
</html>